/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CastleDefense.units;

import CastleDefense.Animation;
import CastleDefense.Game;
import java.awt.Image;
import javax.vecmath.Vector2d;

/**
 *
 * @author Firedemon
 */
public class Archer extends PlacedUnit
{
    private Animation ArcherAnimation;

    public Archer(Vector2d location, Image sprite)
    {
        super(location, sprite, "Archer");

        ArcherAnimation = new Animation(getAnimation("Archer"), 1000);
        this.setAnimation(ArcherAnimation);
    }

    @Override
    public void doLogic()
    {
        if(getTarget() != null && !getTarget().isDead()) //tries to fireAtEnemy target
        {
            targetD = distance(getLoc().x, getLoc().y, getTarget().getMidX(), getTarget().getMidY());
            if(targetD > range)
            {
                setClosestTarget();
                return;
            }

            if(System.currentTimeMillis() - lastFire < rateOfFire)
                return;
            
            lastFire = System.currentTimeMillis();

            //ArcherAnimation.start();
            //if(ArcherAnimation.isDone())
            //{
            //    ArcherAnimation.stop();
                Game.fireAtEnemy(getTarget(), this);
            //}
        }
        else if(getTarget() == null || getTarget().isDead())  //sets target if no target has been set
        {
            setClosestTarget();
        }
        else //resets a target if it is too far away
        {
            setClosestTarget();
        } 
    }
}
